package com.atguigu.gmall.gateway.filter;


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

@Configuration
//配置cors过滤器，解决跨域问题
public class CorsConfig {

    @Bean
    public CorsWebFilter corsWebFilter(){
        //1 配置cors跨域规则
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        //1.1 设置允许哪些域名可以跨域  *：开发测试中方便 项目上线后改为真实服务器地址
        corsConfiguration.addAllowedOrigin("*");
        //1.2 设置是否允许提交cookie
        corsConfiguration.setAllowCredentials(true);
        //1.3 设置允许提交方法 PUT DELETE GET POST
        corsConfiguration.addAllowedMethod("*");
        //1.4 设置允许提交请求头信息
        corsConfiguration.addAllowedHeader("*");
        //1.5 设置预检请求有效期
        corsConfiguration.setMaxAge(3600L);
        //2 注册cors配置
        UrlBasedCorsConfigurationSource configurationSource = new UrlBasedCorsConfigurationSource();
        //第一个参数：设置过滤器拦截请求 第二个参数：cors跨域数据
        configurationSource.registerCorsConfiguration("/**",corsConfiguration);
        return new CorsWebFilter(configurationSource);
    }
}
